<?php
/**
 *
 * UCModTwitterMap model class file
 *
 * @category   UC
 * @author     张丽媛 <zhangliyuan@snda.com>
 * @copyright  2010-2010 SDO UC Group
 * @version    Release: 1.0.0
 * @link       http://www.sdo.com
 * @modified   2010-3-31 20:20
 */

require_once(dirname(__FILE__).'/../../config/UCConfMacro.php');
require_once(dirname(__FILE__).'/../../config/UCConfTwitter.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCache.php');

class UCModTwitterMap{
	private $_source_mood_id;
	private $_mkey_twitter_map;
	private $_cache;
	private $_included = false;


	function __construct($source_mood_id){
		
		$this->_source_mood_id = $source_mood_id;
		$this->_mkey_twitter_map = UCConfTwitter::getMemConveyTwitterMap($source_mood_id);
		$this->_cache = new UCLibCache(TWITTER_MCACHE_CHANNEL_NAME);
	}

	
	function __destruct(){
		$this->_cache->close();
	}

	/**
	 * @purpose: 取得某条记录的转帖映射关系
	 * @params string  source_user:原帖的发表者
	 * @return array or false
	 */
	function getOneTwitterMapInfo($source_user){

		$map_info = $this->_cache->read($this->_mkey_twitter_map);
		$this->_cache->close();
		if (false ===$map_info){
			$pdo = UCLibPDOFactory::getPDO(PDO_MAP_TAGNAME,$this->_source_mood_id,'twitter2dbconfig');
			$map_info = $pdo-> query_with_prepare("select * from $pdo->database.$pdo->table where source_mood_id=? and source_user_id=?",array($this->_source_mood_id,$source_user));
			if (false === $map_info){
				return false;	
			}else if(is_array($map_info) && count($map_info)> 0){
				$this->_cache->write($this->_mkey_twitter_map, $map_info, TWITTER_MCACHE_EXPIRE_TIME);
			    $this->_cache->close();
			}else{
				$map_info = array();
				$this->_cache->write($this->_mkey_twitter_map, $map_info, TWITTER_MCACHE_EXPIRE_TIME);
			    $this->_cache->close();
			}
		}
		return $map_info;
	}

	/**
	 * @purpose: 添加转帖映射关系入映射表中
	 * @params string  source_user:原帖的发表者
	 * @params string  mood_id:转帖后的id
	 * @params string  user_id:转帖的用户
	 * @return true or false
	 */
	function insertIntoTwitterMapDB($source_user,$mood_id,$user_id){
		if(empty($source_user)||empty($mood_id)||empty($user_id)){
			return false;
		}
		$convey_time = date("Y-m-d H:i:s");
		$pdo = UCLibPDOFactory::getPDO(PDO_MAP_TAGNAME,$this->_source_mood_id,'twitter2dbconfig');
		$ret  = $pdo -> exec_with_prepare("INSERT INTO $pdo->database.$pdo->table (source_mood_id,source_user_id,convey_mood_id,convey_user_id,convey_time) VALUES (?,?,?,?,?)",array($this->_source_mood_id,$source_user,$mood_id,$user_id,$convey_time));
	

		return $ret;	
	}
}